import React from 'react';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import { Card, Form, Button, Input, InputNumber, message, Modal, Checkbox, Space } from 'antd';
import { ExclamationCircleOutlined } from '@ant-design/icons';
import { connect } from 'umi';
import JobSelect from '@/components/JobSelect';
import request from '@/utils/request';

const { confirm } = Modal;
const apiURL = '/api/services/app/JobHead/';
class ViewPage extends React.Component<any, any> {
    constructor(props: any) {
        super(props)
        this.state = {
            loading: false,
            data: {},
        }
    }
    componentDidMount() {
    }
    formRef: any = React.createRef();
    onFinish = async (values: any) => {
        var resp = await request(apiURL + 'CheckJob', {
            method: 'POST',
            params: { jobNum: values.jobNum },
        })
        if (!resp.result) {
            let _this = this;
            confirm({
                title: '警告',
                icon: <ExclamationCircleOutlined />,
                content: '工单未全部完成，确认完结工单？',
                onOk() {
                    _this.closeJob(values.id);
                },
            });
        }
        else {
            this.closeJob(values.id);
        }
        return;

    }
    changeCmp = (e: any) => {
        var obj = this.formRef.current.getFieldsValue();
        if(!obj.id){
            message.info('请选选择工单')
            return;
        }
        this.closeJob(obj.id, 'CompleteJob');
    }
    closeJob = (id: any, act: string = 'CloseJob') => {
        this.setState({ loading: true });
        request(apiURL + act, {
            method: 'POST',
            params: { id },
        }).then((response) => {
            if (response) {
                this.setState({ loading: false });
            }
            message.success('工单完结成功')
        });
    }
    getObj = (e: any) => {
        console.log(e);
        this.formRef.current.setFieldsValue(e);
        this.setState({ jobCompleted: e.jobCompleted })
    }
    render() {
        const { loading, jobCompleted } = this.state
        const layout = {
            labelCol: { span: 5 },
            wrapperCol: { span: 10 },
        };
        return (
            <PageHeaderWrapper>
                <Card>
                    <Form {...layout} ref={this.formRef} onFinish={this.onFinish}>
                        <Form.Item name='id' hidden>
                            <Input disabled />
                        </Form.Item>
                        <Form.Item label="工单号">
                            <Input.Group compact>
                                <JobSelect
                                    width={100}
                                    noStyle
                                    name='jobNum'
                                    rules={[{ required: true, message: '工单号必填' }]}
                                    setCurrent={this.getObj}
                                />
                            </Input.Group>
                        </Form.Item>
                        <Form.Item name='partNum' label="物料编码">
                            <Input disabled />
                        </Form.Item>
                        <Form.Item name='partDescription' label="物料描述">
                            <Input.TextArea disabled />
                        </Form.Item>
                        <Form.Item name='prodQty' label="生产数量">
                            <InputNumber disabled />
                        </Form.Item>
                        <Form.Item name='dueDate' label="需求日期">
                            <Input disabled />
                        </Form.Item>
                        <Form.Item name='jobCompleted' wrapperCol={{ offset: 5 }} valuePropName="checked">
                            <Checkbox onChange={this.changeCmp} >工单完成</Checkbox>
                        </Form.Item>
                        <Form.Item wrapperCol={{ offset: 5 }}>
                            <Space><Button type="primary" htmlType="submit" loading={loading}>确定</Button></Space>
                        </Form.Item>
                    </Form>
                </Card>
            </PageHeaderWrapper>
        )
    }
}
export default connect(({ Abp, user }: API.IConnectState) => ({
    response: Abp.response,
    currentUser: user.currentUser,
}))(ViewPage);